package com.dlwx.parttimetalent.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.dlwx.parttimetalent.R;
import com.dlwx.parttimetalent.base.BaseActivity;
import com.dlwx.parttimetalent.bean.BaseBean;
import com.dlwx.parttimetalent.bean.EvaluateBean;
import com.dlwx.parttimetalent.comm.Constant;
import com.dlwx.parttimetalent.comm.Http;
import com.dlwx.parttimetalent.utils.DealResult;
import com.dlwx.parttimetalent.utils.OkgoUtil;
import com.dlwx.parttimetalent.utils.ToastUtils;
import com.google.gson.reflect.TypeToken;
import com.iarcuschin.simpleratingbar.SimpleRatingBar;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;

public class EvaluateActivity extends BaseActivity {

    @BindView(R.id.title_left_one_btn)
    ImageView titleLeftOneBtn;
    @BindView(R.id.tv_middle_title)
    TextView tvMiddleTitle;
    @BindView(R.id.image1)
    CircleImageView image1;
    @BindView(R.id.image2)
    CircleImageView image2;
    @BindView(R.id.ratingbar)
    SimpleRatingBar ratingbar;
    @BindView(R.id.et_content)
    EditText etContent;
    @BindView(R.id.tv_confirm)
    TextView tvConfirm;
    @BindView(R.id.image_reward)
    ImageView imageReward;
    @BindView(R.id.tv_name1)
    TextView tvName1;
    @BindView(R.id.tv_name2)
    TextView tvName2;
    @BindView(R.id.flowlayout)
    TagFlowLayout flowlayout;
    @BindView(R.id.image_name)
    ImageView imageName;
    @BindView(R.id.ll_name)
    LinearLayout llName;
    private String orderId;
    private String orderType;
    private float starts = 3;
    private ArrayList<String> mDatas;
    private List<EvaluateBean.BodyBean.TextlabelBean> labels;
    private boolean isHideName = true;
    private int evatype = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        setContentView(R.layout.activity_evaluate);
        ButterKnife.bind(this);
        initView();
        getDatas();

    }

    private void initView() {
        titleLeftOneBtn.setVisibility(View.VISIBLE);
        tvMiddleTitle.setText("评价");
        tvMiddleTitle.setVisibility(View.VISIBLE);
        Intent intent = getIntent();
        orderId = intent.getStringExtra("id");
        orderType = intent.getStringExtra("type");
        ratingbar.setOnRatingBarChangeListener(new SimpleRatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(SimpleRatingBar simpleRatingBar, float rating, boolean fromUser) {
                starts = rating;
            }
        });
    }

    private void getDatas() {
        loadingDialog.show();
        OkgoUtil.post(Http.GetEvaluateData, EvaluateActivity.this)
                .params("orderType", orderType)
                .params("orderid", orderId)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        loadingDialog.dismiss();
                        EvaluateBean parentBean = DealResult.getInstance().dealDatas(EvaluateActivity.this
                                , response, new TypeToken<EvaluateBean>() {
                                }.getType());
                        if (parentBean == null) {
                            return;
                        }
                        if (parentBean.getCode() == Constant.SUCESS || parentBean.getCode() == Constant.EMPTY) {
                            setDatas(parentBean.getBody());
                        } else {
                            ToastUtils.toastForShort(EvaluateActivity.this, parentBean.getResult());
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        loadingDialog.dismiss();
                        ToastUtils.toastForShort(EvaluateActivity.this, getString(R.string.bad_net));
                    }
                });
    }

    private void setDatas(EvaluateBean.BodyBean body) {
        Glide.with(this).load(body.getHeadurl1()).into(image1);
        Glide.with(this).load(body.getHeadurl2()).into(image2);
        tvName1.setText(body.getNickname1());
        tvName2.setText(body.getNickname2());
        labels = body.getTextlabel();
        TagAdapter<EvaluateBean.BodyBean.TextlabelBean> tagAdapter = new TagAdapter<EvaluateBean.BodyBean.TextlabelBean>(labels) {
            @Override
            public View getView(FlowLayout parent, int position, EvaluateBean.BodyBean.TextlabelBean bean) {
                TextView textView = (TextView) LayoutInflater.from(EvaluateActivity.this)
                        .inflate(R.layout.tv_label_red_select, flowlayout, false);
                textView.getLayoutParams().width = ViewGroup.LayoutParams.WRAP_CONTENT;
                textView.setText(bean.getTextinfo());
                return textView;
            }
        };
        flowlayout.setAdapter(tagAdapter);
    }

    @OnClick({R.id.title_left_one_btn, R.id.tv_confirm, R.id.image_reward, R.id.ll_name})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_left_one_btn:
                this.finish();
                break;
            case R.id.tv_confirm:
                checkInputDatas();
                break;
            case R.id.ll_name:
                if (isHideName) {
                    imageName.setBackgroundResource(R.mipmap.icon_xuanze);
                    isHideName = false;
                    evatype = 2;
                } else {
                    imageName.setBackgroundResource(R.mipmap.icon_xuanzhong);
                    isHideName = true;
                    evatype = 1;
                }
                break;
            case R.id.image_reward:
                Intent i = new Intent(this, RewardActivity.class);
                i.putExtra("id", orderId);
                startActivity(i);
                break;
        }
    }

    private void checkInputDatas() {
        if (starts == 0) {
            ToastUtils.toastForShort(this, "评分不能为零");
            return;
        }
        Set<Integer> sets = flowlayout.getSelectedList();
        if (sets.isEmpty()) {
            ToastUtils.toastForShort(this, "请选择至少一个评价标签");
            return;
        }
        String comments = "";
        for (Integer set : sets) {
            comments = comments + labels.get(set).getTextinfo() + ",";
        }
        comments = comments.substring(0, comments.length() - 1);

        OkgoUtil.post(Http.SubEvaluate, EvaluateActivity.this)
                .params("orderType", orderType)
                .params("orderid", orderId)
                .params("evacontent", comments)
                .params("evascore", starts * 2)
                .params("evatype", evatype)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        BaseBean parentBean = DealResult.getInstance().dealDatas(EvaluateActivity.this, response, new TypeToken<BaseBean>() {
                        }.getType());
                        if (parentBean == null) {
                            return;
                        }
                        int code = parentBean.getCode();
                        if (code == Constant.SUCESS) {
                            EvaluateActivity.this.finish();
                        }
                        ToastUtils.toastForShort(EvaluateActivity.this, parentBean.getResult());

                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        ToastUtils.toastForShort(EvaluateActivity.this, getString(R.string.bad_net));
                    }
                });
    }
}
